package team.stride.database.crud.users;

import java.util.List;

import team.stride.R;
import team.stride.database.StrideDB;
import team.stride.utils.UtilsStride;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class UsersAdapter extends BaseAdapter
{
	private Context context;
	private List<UsersItem> usersList;

	public UsersAdapter(Context context, List<UsersItem> mapslist)
	{
		this.context = context;
		this.usersList = mapslist;
	}

	public int getCount()
	{
		return usersList.size();
	}

	public UsersItem getItem(int position)
	{
		return usersList.get(position);
	}

	public long getItemId(int position)
	{
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup viewGroup)
	{
		UsersItem entry = usersList.get(position);
		if (convertView == null)
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.bd_users_row, null);
		}
		TextView usersName = (TextView) convertView.findViewById(R.id.usersName);
		usersName.setText(entry.getName());

		TextView usersNetwork = (TextView) convertView.findViewById(R.id.usersNetwork);
		usersNetwork.setText("" + entry.getNetwork());

		TextView usersDetails = (TextView) convertView.findViewById(R.id.usersDetails);
		usersDetails.setText("" + entry.getDetails());

		ImageView usersImg = (ImageView) convertView.findViewById(R.id.usersImg);
		usersImg.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				UsersItem element = usersList.remove(position);
				notifyDataSetChanged();
				UtilsStride.removeFromDB(StrideDB.MATCH_U, element);
			}
		});
		return convertView;
	}
}
